package com.huazi.www.yang.login.servlet;


import com.huazi.www.yang.login.entity.DoctorLogin;
import com.huazi.www.yang.login.service.DocdorLoginService;
import com.huazi.www.yang.login.service.Imlp.DocdorLoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class DocdorLoginServlet extends HttpServlet {
    DocdorLoginService service = new DocdorLoginServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String tp = req.getParameter("tp");
        if (tp.equals("login")) {
            String doctorId = req.getParameter("userName");
            String pwd = req.getParameter("userPwd");
            boolean bool = service.login(doctorId, pwd);
            if (bool) {
                DoctorLogin doctor = service.doctor(doctorId);
                req.getSession().setAttribute("doctor", doctor);
                req.getSession().setAttribute("doctorId", doctorId);
                req.getRequestDispatcher("menu.jsp").forward(req, resp);
            } else {
                req.setAttribute("failing", "failing");
                req.getRequestDispatcher("docdorLogin/login.jsp").forward(req, resp);
            }
        } else if (tp.equals("selectSfz")) {
            String doctorId = req.getParameter("doctorId");//医生id
            String sfz = req.getParameter("sfz");//身份证
            boolean bool = service.forgetPwd(doctorId, sfz);
            PrintWriter out = resp.getWriter();
            out.print(bool);
            out.flush();
            out.close();

        } else if (tp.equals("updatePwd")) {
            String doctorId = req.getParameter("doctorId");//医生id
            String pwd = req.getParameter("pwd");
            service.updatePwd(doctorId, pwd);
            req.getRequestDispatcher("docdorLogin/login.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
